package com.miracle9.game.util;

import com.miracle9.game.bean.AdminRecording;
import com.wmcy.bi.collect.CenterBISender;
import com.wmcy.bi.collect.RechargeGain;
import com.wmcy.bi.collect.RunStatistics;
import com.wmcy.bi.collect.SaleSet;
import com.wmcy.bi.collect.SubordinateNumber;
import com.wmcy.bi.collect.UserOnLineDuration;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;

public class ProcessObjectStream
{
  private static final Logger logger = Logger.getLogger(ProcessObjectStream.class);
  public static int ar = 103;
  public static String as = "/home/cs/";
  private static List<AuxiliaryCodingKey> at = new ArrayList();
  private static FileOutputStream au;
  private static FileInputStream av;
  private static ObjectOutputStream oos;
  private static ObjectInputStream ois;
  static
  {
    try
    {
      String today = DateDifferenceProcess.getDate();
      File f = new File(as + "system" + today + ".data");
      if (!f.exists())
      {
        au = new FileOutputStream(as + "system" + today + ".data", true);
        oos = new ObjectOutputStream(au);
      }
      else
      {
        f.delete();
        au = new FileOutputStream(as + "system" + today + ".data", true);
        oos = new ObjectOutputStream(au);
      }
      av = new FileInputStream(as + "system" + today + ".data");
      ois = new ObjectInputStream(av);
    }
    catch (Exception e)
    {
      logger.error("CenterBIUtil error:" + e);
    }
  }
  
  public static SaleSet setSaleSet(int serverId, String userRechargeRate, String directProRechargeRate)
  {
    SaleSet saleSet = new SaleSet();
    saleSet.setServerId(serverId);
    saleSet.setUserRechargeRate(userRechargeRate);
    saleSet.setDirectProRechargeRate(directProRechargeRate);
    saleSet.setLastSetTime(System.currentTimeMillis());
    return saleSet;
  }
  
  public static RechargeGain setRechargeGain(int serverId, int userId, int operation, int currency)
  {
    RechargeGain rechargeGain = new RechargeGain();
    rechargeGain.setServerId(serverId);
    rechargeGain.setUserId(userId);
    rechargeGain.setOperation(operation);
    rechargeGain.setCurrency(currency);
    rechargeGain.setDate(System.currentTimeMillis());
    return rechargeGain;
  }
  
  public static RunStatistics setRunStatistics(int serverId, int totalWan, int totalDe, int totalGain, int gameType, long dateTime)
  {
    RunStatistics runStatistics = new RunStatistics();
    runStatistics.setServerId(serverId);
    runStatistics.setTotalWan(totalWan);
    runStatistics.setTotalDe(totalDe);
    runStatistics.setTotalGain(totalGain);
    runStatistics.setGameType(gameType);
    runStatistics.setDate(dateTime);
    return runStatistics;
  }
  
  public static SubordinateNumber setSubordinateNumber(int serverId, int directUser, int notDirectUser, int directPromoter, int notDirectPromoter)
  {
    SubordinateNumber subordinateNumber = new SubordinateNumber();
    subordinateNumber.setServerId(serverId);
    subordinateNumber.setDirectUser(directUser);
    subordinateNumber.setNoDirectUser(notDirectUser);
    subordinateNumber.setDirectPromoter(directPromoter);
    subordinateNumber.setNoDirectPromoter(notDirectPromoter);
    return subordinateNumber;
  }
  
  public static UserOnLineDuration setUserOnLineDuration(int userId, int serverId, long loginTime, long loginOutTime, long duration)
  {
    UserOnLineDuration userOnLineDuration = new UserOnLineDuration();
    userOnLineDuration.setUserId(userId);
    userOnLineDuration.setServerId(serverId);
    userOnLineDuration.setLoginTime(loginTime);
    userOnLineDuration.setLoginOutTime(loginOutTime);
    userOnLineDuration.setDuration(duration);
    return userOnLineDuration;
  }
  
  @Deprecated
  public static void sendSaleSet(SaleSet saleSet)
  {
    logger.info("updateSaleSet begin");
    logger.info("Saleset serverId:" + saleSet.getServerId());
    CenterBISender.getInstance().sendSaleSet(saleSet);
    logger.info("updateSaleSet end");
  }
  
  @Deprecated
  public static void sendRechargeGain(RechargeGain rechargeGain)
  {
    logger.info("addRechargeGain begin");
    logger.info("RechargeGain userId:" + rechargeGain.getUserId());
    CenterBISender.getInstance().sendRechargeGain(rechargeGain);
    logger.info("addRechargeGain end");
  }
  
  @Deprecated
  public static void sendRunStatistics(RunStatistics runStatistics)
  {
    logger.info("addRunStatistics begin");
    logger.info("RunStatistics" + runStatistics.getServerId() + "," + 
      runStatistics.getDate() + "," + runStatistics.getGameType() + 
      "," + runStatistics.getTotalWan() + "," + 
      runStatistics.getTotalDe() + "," + 
      runStatistics.getTotalGain());
    CenterBISender.getInstance().sendRunStatistics(runStatistics);
    logger.info("addRunStatistics end");
  }
  
  @Deprecated
  public static void sendSubordinateNumber(SubordinateNumber subordinateNumber)
  {
    logger.info("updateSubordinateNumber begin");
    logger.info("SubordinateNumber serverId:" + subordinateNumber.getServerId());
    CenterBISender.getInstance().sendSubordinateNumber(subordinateNumber);
    logger.info("updateSubordinateNumber end");
  }
  
  public static void AdminRecordingWriteObject(UserOnLineDuration userOnLineDuration)
  {
    logger.info("addUserOnLineDuration begin");
    AdminRecording serializableUserOnLineDuration = new AdminRecording();
    serializableUserOnLineDuration.setDuration(userOnLineDuration.getDuration());
    serializableUserOnLineDuration.setLoginOutTime(userOnLineDuration.getLoginOutTime());
    serializableUserOnLineDuration.setLoginTime(userOnLineDuration.getLoginTime());
    serializableUserOnLineDuration.setServerId(userOnLineDuration.getServerId());
    serializableUserOnLineDuration.setUserId(userOnLineDuration.getUserId());
    logger.info("userId:" + serializableUserOnLineDuration.getUserId() + ",serverId:" + serializableUserOnLineDuration.getServerId() + 
      ",loginTime:" + serializableUserOnLineDuration.getLoginTime() + ",logoutTime:" + serializableUserOnLineDuration.getLoginOutTime() + 
      ",duration:" + serializableUserOnLineDuration.getDuration());
    try
    {
      oos.writeObject(serializableUserOnLineDuration);
      oos.flush();
    }
    catch (Exception ex)
    {
      logger.error("addUserOnLineDuration Exception:" + ex);
    }
    logger.info("addUserOnLineDuration end");
  }
  
  public static void sendUserOnLineDuration()
  {
    logger.info("sendUserOnLineDuration begin");
    try
    {
      String today = DateDifferenceProcess.getDate();
      
      logger.info("today:" + today);
      File f = new File(as + "system" + today + ".data");
      if (!f.exists())
      {
        oos.close();
        au.close();
        au = new FileOutputStream(as + "system" + today + ".data", true);
        oos = new ObjectOutputStream(au);
      }
      AdminRecording serializableUserOnLineDuration = null;
      List<UserOnLineDuration> userOnLineDurations = new ArrayList();
      try
      {
        while ((serializableUserOnLineDuration = (AdminRecording)ois
          .readObject()) != null)
        {
          UserOnLineDuration userOnLineDuration = new UserOnLineDuration();
          userOnLineDuration.setDuration(serializableUserOnLineDuration.getDuration());
          userOnLineDuration.setLoginOutTime(serializableUserOnLineDuration.getLoginOutTime());
          userOnLineDuration.setLoginTime(serializableUserOnLineDuration.getLoginTime());
          userOnLineDuration.setServerId(serializableUserOnLineDuration.getServerId());
          userOnLineDuration.setUserId(serializableUserOnLineDuration.getUserId());
          userOnLineDurations.add(userOnLineDuration);
        }
      }
      catch (Exception e)
      {
        logger.error("read EOF:" + e);
      }
      for (UserOnLineDuration uod : userOnLineDurations) {
        logger.info("duration:" + uod.getDuration());
      }
      CenterBISender.getInstance().sendSomeUserOnLineDuration(userOnLineDurations);
      ois.close();
      av.close();
      
      String yesterday = DateDifferenceProcess.getDateYMD(System.currentTimeMillis() - 86400000L);
      
      logger.info("yesterday:" + yesterday);
      File f2 = new File(as + "system" + yesterday + ".data");
      if (f2.exists()) {
        f2.delete();
      }
      av = new FileInputStream(as + "system" + today + ".data");
      ois = new ObjectInputStream(av);
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
    logger.info("sendUserOnLineDuration end");
  }
}
